<template>
  <div style="width:100%;" class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="单位名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.businessLicense" placeholder="请输入统一社会编码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('handle.search') }}</el-button>
        <el-button  type="primary" @click="addOrUpdateHandle()">{{ $t('handle.add') }}</el-button>
        <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">{{ $t('handle.batchDelete') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      ref="table"
      border
      style="width: 100%"
      height="500"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        >
      </el-table-column>
      <!-- <el-table-column
        prop="unitCode"
        header-align="center"
        align="center"
        label="单位编码">
      </el-table-column> -->
       <!-- <el-table-column
        prop="addressVo.addressName"
        header-align="center"
        :show-overflow-tooltip="true"
        align="center"
        label="标准地址">
      </el-table-column> -->
      <el-table-column
        prop="name"
        header-align="center"
        :show-overflow-tooltip="true"
        align="center"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="businessLicense"
        header-align="center"
        align="center"
        label="统一社会编码">
      </el-table-column>
      <el-table-column
        prop="institutions"
        header-align="center"
        align="center"
        label="属性">
        <!-- <template slot-scope="scope">
          <span>{{dwsxType(scope.row.institutions)}}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        prop="businessCategory"
        header-align="center"
        align="center"
        label="类别">
        <!-- <template slot-scope="scope">
          <span>{{qylbType(scope.row.businessCategory)}}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        prop="typeBusiness"
        header-align="center"
        align="center"
        label="注册类型">
        <!-- <template slot-scope="scope">
          <span>{{qyczlxType(scope.row.typeBusiness)}}</span>
        </template> -->
      </el-table-column>
      <!-- <el-table-column
        prop="holdings"
        header-align="center"
        align="center"
        label="控股情况">
        <template slot-scope="scope">
          <span>{{kgqkType(scope.row.holdings)}}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="businessScope"
        header-align="center"
        align="center"
        label="经营范围">
        <!-- <template slot-scope="scope">
          <span>{{jyfwType(scope.row.businessScope)}}</span>
        </template> -->
      </el-table-column>
      <!-- <el-table-column
        prop="monitoring"
        header-align="center"
        align="center"
        label="是否有监控">
        <template slot-scope="scope">
          <span>{{monitoringType(scope.row.monitoring)}}</span>
        </template>
      </el-table-column> -->
      <!--<el-table-column
        prop="businessLicense"
        header-align="center"
        align="center"
        label="营业执照号">
      </el-table-column>
       <el-table-column
        prop="addressId"
        header-align="center"
        align="center"
        label="标准地址id">
      </el-table-column> -->
      <el-table-column
        header-align="center"
        align="center"
        :label="$t('handle.handle')">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.unitCode,true)">{{ $t('handle.detail') }}</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.unitCode)">{{ $t('handle.update') }}</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.unitCode)">{{ $t('handle.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './add-or-update.vue'
  import { getDictList } from '@/utils'
  export default {
    data () {
      return {
        dataForm: {
          key: '',
          businessLicense: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        dwsxOptions: [], // 单位属性数据
        qylbOptions: [], // 单位类别数据
        qyczlxOptions: [], // 单位注册类型数据
        kgqkOptions: [], // 控股情况数据
        jyfwOptions: [], // 经营范围数据
        monitoringOptions: [] // 是否有监控数据
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      // this.getDataList()

    },
    created () {
      // this.getDataList()
    },
    computed: {
      regionId() {
        return this.$store.state.address.regionId;
      }
      // qylbType() {
      //   return function (type) {
      //     return this.qylbOptions[type]
      //   }
      // },
      // dwsxType() {
      //   return function (type) {
      //     return this.dwsxOptions[type]
      //   }
      // },
      // qyczlxType() {
      //   return function (type) {
      //     return this.qyczlxOptions[type]
      //   }
      // },
      // jyfwType() {
      //   return function (type) {
      //     return this.jyfwOptions[type]
      //   }
      // },
      // kgqkType() {
      //   return function (type) {
      //    return this.kgqkOptions[type]
      //   }
      // },
      // monitoringType() {
      //   return function (type) {
      //    return this.monitoringOptions[type]
      //   }
      // }
    },
    methods: {
      adjust(){
        setTimeout(()=>{
          this.$refs['table'].doLayout()
        },100)
      },
      initData() {
        getDictList('business_category').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.qylbOptions[data.dictList[i].value] = data.dictList[i].label
          }
        })
        getDictList('institutions').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.dwsxOptions[data.dictList[i].value] = data.dictList[i].label
          }
        })
        getDictList('type_business').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.qyczlxOptions[data.dictList[i].value] = data.dictList[i].label
          }
        })
        getDictList('holdings').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.kgqkOptions[data.dictList[i].value] = data.dictList[i].label
          }
        })
        getDictList('business_scope').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.jyfwOptions[data.dictList[i].value] = data.dictList[i].label
          }
        })
        getDictList('monitoring').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.monitoringOptions[data.dictList[i].value] = data.dictList[i].label
          }
          this.initDataList()
        })
      },
      // 获取数据列表
      getDataList () {
        this.initData()
      },
      initDataList() {
        this.dataListLoading = true
        let _this = this
        this.$http({
          url: '/dg-standard-info/dgregisterunit/list',
          method: 'post',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.key,
            'businessLicense': this.dataForm.businessLicense,
            'regionId':this.regionId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            data.page.list.map(function (item) {
              item.institutions = _this.dwsxOptions[item.institutions]
              item.businessCategory = _this.qylbOptions[item.businessCategory]
              item.typeBusiness = _this.qyczlxOptions[item.typeBusiness]
              item.holdings = _this.kgqkOptions[item.holdings]
              item.businessScope = _this.jyfwOptions[item.businessScope]
              item.monitoring = _this.monitoringOptions[item.monitoring]
              return item
            })
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
          this.dataListLoading = false
        },
          error => {
            if (error.response.status === 403) {
              this.$message.error("你没有权限");
            } else {
              this.$message.error("系统错误，请联系管理员");
            }
          })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id ,detail) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, detail)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.unitCode
        })
        this.$confirm(`确定进行删除操作嘛？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            // url: this.$http.adornUrl('/dgactualunit/delete'),
             url: '/dg-standard-info/dgregisterunit/batchdelete',
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style>
#pane-five{
  overflow: auto;
  height: 100%;
}
</style>

